package com.lurencun.android.topicbank.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class CommonUtil {

	private static final String TAG = CommonUtil.class.getSimpleName();

	/**
	 * @brief 判读输入的字符串参数是否为空
	 * @param str
	 *            输入的字符参数
	 * @return 返回字符串是否为空 true：字符串为空 false：字符串不为空
	 */
	public static boolean isNotNull(String str) {
		if (str != null && str.length() != 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * @brief 判读字符串是否为空
	 * @param str
	 *            要判断的字符串
	 * @return 为空返回true 不为空返回false
	 */
	public static boolean isNull(String str) {
		if (str == null || str.length() == 0) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean isNetworkAvailable(Context context) {
		try {
			ConnectivityManager cm = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo info = cm.getActiveNetworkInfo();
			if (info != null) {
				if (info.isConnected()) {
					Log.i(TAG, "Network is connected");
					return true;
				} else {
					Log.i(TAG, "Network is not connected");
					return false;
				}
			}
			return false;
		} catch (Exception e) {
			Log.e(TAG, "get NetworkInfo error." + e.getMessage(), e);
			return false;
		}
	}
}
